﻿using System;
using System.Collections.Generic;
using System.Data;
using eTMS.Controllers.HoSoChiTietService;
using eTMS.Controllers.HoSoService;
namespace eTMS.Controllers
{
    public class HoSoController : HoSoControllerBase
    {
        public HoSoController()
            : base()
        {
        }

        private DataTable tblDetailRemoved = null;
        public DataTable tblDataDetail;
        public void LoadDataChiTiet()
        {
            HoSoChiTietService.HoSoChiTietServiceClient hoSoChiTietServiceClient = new HoSoChiTietServiceClient();
            HoSoChiTietService.HoSoChiTietEntity[] list = hoSoChiTietServiceClient.SelectAllFilter("HoSo_ID="+HoSoObject.HoSo_ID.ToString());
            InitTableDetail();
            LoaiGiayToController loaiGiayToController = new LoaiGiayToController();
            foreach (HoSoChiTietEntity hoSoChiTietEntity in list)
            {
                loaiGiayToController.LoadObject(hoSoChiTietEntity.LoaiGiayTo_ID);
                DataRow row = tblDataDetail.NewRow();
                row["HoSoChiTiet_ID"] = hoSoChiTietEntity.HoSoChiTiet_ID;
                row["SoLuongYeuCau"] = hoSoChiTietEntity.SoLuongYeuCau;
                row["LoaiGiayTo_ID"] = hoSoChiTietEntity.LoaiGiayTo_ID;
                row["MaLoaiGiayTo"] = loaiGiayToController.LoaiGiayToObject.MaLoaiGiayTo;
                row["TenLoaiGiayTo"] = loaiGiayToController.LoaiGiayToObject.TenLoaiGiayTo;
                row["BatBuoc"] = hoSoChiTietEntity.BatBuoc;
                tblDataDetail.Rows.Add(row);
            }
        }
        public void InitTableDetail()
        {
            tblDataDetail = new DataTable();
            tblDataDetail.Columns.Add("HoSoChiTiet_ID", typeof(int));
            tblDataDetail.Columns.Add("SoLuongYeuCau", typeof(int));
            tblDataDetail.Columns.Add("LoaiGiayTo_ID", typeof(int));
            tblDataDetail.Columns.Add("MaLoaiGiayTo", typeof(string));
            tblDataDetail.Columns.Add("TenLoaiGiayTo", typeof(string));
            tblDataDetail.Columns.Add("BatBuoc", typeof(bool));

        }
        public bool IsExistsLoaiGiayTo(int LoaiGiayTo_ID)
        {
            if (tblDataDetail == null)
            {
                InitTableDetail();
            }
            if (tblDataDetail.Rows.Count == 0) return false;
            if (tblDataDetail.Select("LoaiGiayTo_ID=" + LoaiGiayTo_ID.ToString()).Length > 0) return true;
            return false;
        }
        public List<int> GetListLoaiGiayTo()
        {
            List<int> ls = new List<int>();
            if (tblDataDetail == null)
                InitTableDetail();
            foreach (DataRow dataRow in tblDataDetail.Rows)
            {
                ls.Add(ConvertInt(dataRow["LoaiGiayTo_ID"]));
            }
            return ls;
        }
        public void AddLoaiGiayTo(List<int> ls)
        {
            if (tblDataDetail == null)
                InitTableDetail();
            LoaiGiayToController loaiGiayToController = new LoaiGiayToController();
            foreach (int loaiGiayTo_ID in ls)
            {
                loaiGiayToController.LoadObject(loaiGiayTo_ID);
                if (loaiGiayToController.LoaiGiayToObject != null)
                {
                    DataRow row = tblDataDetail.NewRow();
                    row["SoLuongYeuCau"] = 1;
                    row["LoaiGiayTo_ID"] = loaiGiayToController.LoaiGiayToObject.LoaiGiayTo_ID;
                    row["MaLoaiGiayTo"] = loaiGiayToController.LoaiGiayToObject.MaLoaiGiayTo;
                    row["TenLoaiGiayTo"] = loaiGiayToController.LoaiGiayToObject.TenLoaiGiayTo;
                    row["BatBuoc"] = true;
                    tblDataDetail.Rows.Add(row);
                }
            }
        }
        public void RemoveLoaiGiayTo(List<DataRow> ls)
        {
            if (tblDetailRemoved == null)
                tblDetailRemoved = tblDataDetail.Clone();
            foreach (DataRow row in ls)
            {
                tblDataDetail.Rows.Remove(row);
                tblDetailRemoved.ImportRow(row);
            }
        }
        public void SaveDetail()
        {
            HoSoChiTietService.HoSoChiTietServiceClient hoSoChiTietServiceClient = new HoSoChiTietServiceClient();
            if (tblDetailRemoved != null)
            {
                foreach (DataRow row in tblDetailRemoved.Rows)
                {
                    hoSoChiTietServiceClient.DeleteByID(ConvertInt(row["HoSoChiTiet_ID"]));
                }
            }
            if (tblDataDetail != null)
            {
                
                foreach (DataRow row in tblDataDetail.Rows)
                {
                    HoSoChiTietService.HoSoChiTietEntity oChiTiet=new HoSoChiTietEntity();
                    oChiTiet.LoaiGiayTo_ID = ConvertInt(row["LoaiGiayTo_ID"]);
                    oChiTiet.SoLuongYeuCau = ConvertInt(row["SoLuongYeuCau"]);
                    oChiTiet.HoSo_ID = HoSoObject.HoSo_ID;
                    oChiTiet.HoSoChiTiet_ID = ConvertInt(row["HoSoChiTiet_ID"]);
                    oChiTiet.BatBuoc = ConvertBool(row["BatBuoc"]);
                    if(oChiTiet.HoSoChiTiet_ID>0)
                    {
                        hoSoChiTietServiceClient.Update(oChiTiet);
                    }
                    else
                    {
                        hoSoChiTietServiceClient.Save(oChiTiet);
                    }
                }
            }
        }
        public List<HoSoChiTietService.HoSoChiTietEntity>LoadAllHoSoChiTietNhapHoc()
        {
            List<HoSoEntity> lsHoSo = ListHoSoStatic.FindAll(delegate(HoSoEntity oFind) { return oFind.NopKhiNhapHoc; });
            HoSoChiTietServiceClient hoSoChiTietServiceClient=new HoSoChiTietServiceClient();
            if(lsHoSo.Count==0)return new List<HoSoChiTietService.HoSoChiTietEntity>();
            List<HoSoChiTietService.HoSoChiTietEntity> lsHoSoChiTiet = new List<HoSoChiTietEntity>(hoSoChiTietServiceClient.SelectAllFilter("HoSo_ID IN(" + string.Join(",", lsHoSo.ConvertAll<string>(delegate(HoSoEntity oConvert) { return oConvert.HoSo_ID.ToString(); }).ToArray()) + ")"));
            return lsHoSoChiTiet;
        }
    }
}

